package com.mebs.model.logistics;

import org.jt.jet.util.JetUtils;

import javax.persistence.*;

import java.util.*;

import com.mebs.model.inventory.AbstractOrder;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Feb 22, 2006, 9:00:28 PM
 */
@Entity()
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "OCEAN")
@PrimaryKeyJoinColumn(name = "Id")
public class Ocean extends Transportation {
    @ManyToOne(optional = true)
    @JoinColumn(name = "StatusId")
    private OceanStatus status;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "OceanId")
    @OrderBy("number")
    private Set<OceanContainer> containers;
    
    @SuppressWarnings("unused")
	@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    @JoinColumn(name = "OceanId")
    private Set<StatusHistory> oceanStatusHistories;

    /**
     * Getters-setters
     */

    public OceanStatus getStatus()
    {
        return status;
    }

    public void setStatus(OceanStatus status)
    {
        this.status = status;
    }

    public Set<OceanContainer> getContainers()
    {
        return containers;
    }

    public void setContainers(Set<OceanContainer> containers)
    {
        this.containers = containers;
    }

    @Transient
    public boolean isOrderExcludable(AbstractOrder order) {
        boolean excludable = true;
        for(OceanContainer container : this.containers) {
            if(container.isOrderUsed(order)) {
                excludable = false;
                break;
            }
        }
        return excludable;
    }

    @Transient
    public Type getRequirementType()
    {
        return Type.OCEAN;
    }

    @Transient
    public boolean isDeletable()
    {
        return (super.isDeletable()
            && JetUtils.isEmpty(this.containers));
    }

    @Transient
	public String getFirstContainerNumber() {
		Set<OceanContainer> containers = getContainers();
		String contNum = "";
		if(containers != null && containers.size() != 0){
			contNum = (containers.iterator().next()).getNumber();	
		}

		return contNum;
	}

	@Transient
	public int getContainersCount() {
		return getContainers() != null ? getContainers().size() : 0;
	}

	@Override
    public String toString()
    {
        return (this.getSequence() != null) ? this.getSequence().toString() : "";
    }



//    @Transient
//    public String getSequenceNumber()
//    {
//        Calendar c = new GregorianCalendar();
//        c.setTime(getStartDate());
//        return String.format("OCEAN-%s-%05d-%02d%02d%02d",
//            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
//            getSequence(),
//            c.get(Calendar.YEAR) % 100,
//            c.get(Calendar.MONTH) + 1,
//            c.get(Calendar.DAY_OF_MONTH));
//    }
}
